package com.topdt.jbpm.web.struts.action;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.CurdAction;
import com.thx.common.web.ActionResult;
import com.thx.log.LogUtil;
import com.topdt.jbpm.model.ArcTemplate;
import com.topdt.jbpm.service.ArcTemplateService;
import com.topdt.jbpm.utils.ModuleConstant;

/**  
 *  @Project       : thx-workflow
 *  @Program Name  : com.topdt.jbpm.web.struts.action.TemplateAction.java
 *  @Class Name    : TemplateAction
 *  @Description   : 公文管理/模板
 *  @Author        : calonlan
 *  @Creation Date : 2015年4月14日 上午8:59:50 
 *  @ModificationHistory  
 *  Who        When          What 
 *  --------   ----------    -----------------------------------
 *  username   2015年4月14日       TODO
 */

@Namespace("/workflow")
@Action(value = "template", results = {
		@Result(name = "list", location = "/workflow/webOffice/list.jsp"),
		@Result(name = "input", location = "/workflow/webOffice/input.jsp"),
		@Result(name = "json", type = "json", params = { "root", "result" })
})
@Controller
@Scope("prototype")
public class ArcTemplateAction extends CurdAction<ArcTemplate> {
	
	private static final long serialVersionUID = -1487193945706727798L;

	@Autowired
	private ArcTemplateService arcTemplateService;
	
	private String[] templateIds;//zhangyong 20160129 模板id
	
	private String[] templateOrders;//zhangyong 20160129 模板排序
	
	@Override
	protected void doList() throws Exception {
		logger.debug("<进入>:doList方法");
		list = arcTemplateService.findByName(entity.getName());
	}

	@Override
	protected void doDelete() throws Exception {
		logger.debug("<进入>:doDelete方法   删除模板");
		if (StringUtils.isNotBlank(id)) {
			arcTemplateService.delete(Integer.parseInt(id));
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log(ModuleConstant.TEMPLATE, LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除模板");
		} else {
			setResult(ActionResult.FAIL, "服务器异常");
		}
	}

	public String update() {
		logger.debug("<进入>:update方法   更新模板");
		String recordId = (String) getRequest().getParameter("recordId");
		String name = (String) getRequest().getParameter("name");
		String descript = (String) getRequest().getParameter("descript");
		List<ArcTemplate> list = arcTemplateService.findByRecordId(recordId);
		if (list.size() <= 0) {
			setResult(ActionResult.FAIL, "要修改的模板不存在！");
		} else {
			ArcTemplate template = list.get(0);
			template.setName(name);
			template.setDescript(descript);
			arcTemplateService.update(template);
			setResult(ActionResult.SUCCESS, "保存模板成功！");
		}
		return JSON;
	}

	@Override
	protected void doDelMulti() throws Exception {
		logger.debug("<进入>:doDelMulti方法   删除模板");
		if (delId != null && delId.length > 0) {
			delId = delId[0].split(",");
			for (String id : delId) {
				arcTemplateService.delete(Integer.parseInt(id));
			}
			setResult(ActionResult.SUCCESS, "删除模板成功！");
			LogUtil.log(ModuleConstant.TEMPLATE, LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除模板");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}
	
	/**
	 * 保存模板的显示顺序
	 * @author zhangyong
	 * @date   2016年1月29日 上午11:46:21
	 */
	public void saveOrder(){
		logger.debug("<进入>:saveOrder方法   保存模板的显示顺序");
		if(templateIds != null && templateIds.length > 0){
			try {
				templateIds = templateIds[0].split(",");
				templateOrders = templateOrders[0].split(",");
				for(int i = 0; i < templateIds.length; i++){
					ArcTemplate arcTemplate = arcTemplateService.get(Integer.valueOf(templateIds[i]));
					if(arcTemplate != null){
						arcTemplate.setTemplateOrder(Long.parseLong(templateOrders[i]));
						arcTemplateService.saveOrUpdate(arcTemplate);
					}
				}
				writeJson("success");
			} catch (IOException e) {
				try {
					writeJson("fail");
				} catch (IOException e1) {
					logger.error("<错误>:保存模板的显示顺序失败",e1);
				}
				logger.error("<错误>:保存模板的显示顺序失败",e);
			}
		}
	}
	
	public String[] getTemplateIds() {
		return templateIds;
	}

	public void setTemplateIds(String[] templateIds) {
		this.templateIds = templateIds;
	}

	public String[] getTemplateOrders() {
		return templateOrders;
	}

	public void setTemplateOrders(String[] templateOrders) {
		this.templateOrders = templateOrders;
	}
	
}
